package com.jgads.ad.sdk.mediation.adapter;

import android.util.Log;
import android.view.View;

import com.bytedance.sdk.openadsdk.mediation.MediationConstant;
import com.bytedance.sdk.openadsdk.mediation.bridge.custom.native_ad.MediationCustomNativeAd;
import com.jgads.ad.sdk.mediation.utils.ViewUtil;
import com.junion.ad.bean.NativeExpressAdInfo;
import com.junion.ad.listener.VideoListener;
import com.junion.ad.model.INativeAd;

/**
 * @Description:
 * @Author: maipian
 * @CreateDate: 12/24/21 9:35 AM
 */
public class JUnionNativeExpressAd extends MediationCustomNativeAd {

    private NativeExpressAdInfo nativeExpressAdInfo;
    private int biddingType;

    public NativeExpressAdInfo getNativeExpressAdInfo() {
        return nativeExpressAdInfo;
    }

    public JUnionNativeExpressAd(NativeExpressAdInfo nativeExpressAdInfo, int biddingType) {
        this.nativeExpressAdInfo = nativeExpressAdInfo;
        this.biddingType = biddingType;
        setExpressAd(true);
        nativeExpressAdInfo.setVideoListener(new VideoListener() {
            @Override
            public void onVideoStart(INativeAd iNativeAd) {
                callVideoStart();
            }

            @Override
            public void onVideoFinish(INativeAd iNativeAd) {
                callVideoCompleted();
            }

            @Override
            public void onVideoPause(INativeAd iNativeAd) {
                callVideoPause();
            }

            @Override
            public void onVideoError(INativeAd iNativeAd) {
                callVideoError(-1, "视频播放失败");
            }
        });
    }

    @Override
    public View getExpressView() {
        Log.i(Const.TAG, "getExpressView " + Thread.currentThread().toString());
        if (nativeExpressAdInfo != null) {
            return nativeExpressAdInfo.getNativeExpressAdView();
        }
        return null;
    }

    @Override
    public void render() {
        Log.i(Const.TAG, "render " + Thread.currentThread().toString());
        if (nativeExpressAdInfo != null) {
            callRenderSuccess(MediationConstant.FULL_WIDTH, MediationConstant.AUTO_HEIGHT);
            if (isClientBidding() && !nativeExpressAdInfo.isReportBidWin()) {
                nativeExpressAdInfo.sendWinNotice(nativeExpressAdInfo.getBidPrice());
            }
            nativeExpressAdInfo.render();
        }
    }

    @Override
    public boolean hasDislike() {
        return true;
    }

    @Override
    public MediationConstant.AdIsReadyStatus isReadyCondition() {
        if (nativeExpressAdInfo != null && !nativeExpressAdInfo.isOvertime()) {
            return MediationConstant.AdIsReadyStatus.AD_IS_READY;
        } else {
            if (nativeExpressAdInfo != null && nativeExpressAdInfo.isOvertime()) {
                return MediationConstant.AdIsReadyStatus.AD_IS_EXPIRED;
            }
            return MediationConstant.AdIsReadyStatus.AD_IS_NOT_READY;
        }
    }

    /**
     * 是否clientBidding广告
     *
     * @return
     */
    public boolean isClientBidding() {
        return biddingType == MediationConstant.AD_TYPE_CLIENT_BIDING;
    }


    public void clear() {
        ViewUtil.removeSelfFromParent(getExpressView());
    }


}
